using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using InfoExpress.HIV.Services;
using InfoExpress.HIV.Domains;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.WinApp.Utils;

namespace InfoExpress.HIV.WinApp.Popups
{
  public partial class FrmDkDieuTriOI : XtraForm
  {
    #region Members and Properties

    /// <summary>
    /// Ma benh nhan.
    /// </summary>
    public string MaBenhNhan { get; set; }
    /// <summary>
    /// Ngay dang ky.
    /// </summary>
    public DateTime NgayDangKy { get; set; }
    /// <summary>
    /// Benh nhan Id.
    /// </summary>
    public int BenhNhanId { get; set; }

    #endregion

    public FrmDkDieuTriOI()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmDkDieuTriOI_Load(object sender, EventArgs e)
    {
      try
      {
        DataSet dtsBenhNhan = GlobalCache.BenhNhanDangKyService.LayThongTinDayDu(BenhNhanId);
        DataTable dtbBenhNhan = dtsBenhNhan.Tables[0];
        if (dtbBenhNhan.Rows.Count > 0)
        {
          DataRow dataRow = dtbBenhNhan.Rows[0];
          #region ThongTinCaNhan
          txtHoTen.EditValue = dataRow["TenBenhNhan"];
          rgGioiTinh.EditValue = dataRow["GioiTinh"];
          txtCmnd.EditValue = dataRow["Cmnd"];
          txtNoiCapCmnd.EditValue = dataRow["NoiCapCmnd"];
          deNgayCapCmnd.EditValue = dataRow["NgayCapCmnd"];
          deNgaySinh.EditValue = dataRow["NgaySinh"];
          if (dataRow["HinhAnh"] != DBNull.Value)
            peHinhAnh.Image = UIUtils.ByteArrayToImage((byte[])dataRow["HinhAnh"]);
          #endregion
          #region DiaChi
          txtThTruTp.EditValue = dataRow["TenThTruTp"];
          txtThTruQuan.EditValue = dataRow["TenThTruQuan"];
          txtThTruPhuong.EditValue = dataRow["TenThTruPhuong"];
          txtThTruDuong.EditValue = dataRow["ThTruDuong"];
          txtTmTruTp.EditValue = dataRow["TenTmTruTp"];
          txtTmTruQuan.EditValue = dataRow["TenTmTruQuan"];
          txtTmTruPhuong.EditValue = dataRow["TenTmTruPhuong"];
          txtTmTruDuong.EditValue = dataRow["TmTruDuong"];
          #endregion
          #region ThongTinCapThe
          txtDienThoai.EditValue = dataRow["DienThoai"];
          txtEmail.EditValue = dataRow["Email"];
          txtDonVi.EditValue = dataRow["TenDonVi"];
          txtMaThe.EditValue = dataRow["MaBenhNhan"];
          deNgayXinCapThe.EditValue = dataRow["NgayDangKy"];
          txtUserNhap.EditValue = dataRow["UserNhap"];
          deNgayNhap.EditValue = dataRow["NgaySua"];
          meGhiChu.EditValue = dataRow["GhiChu"];

          DataSet dtsDieuTri = GlobalCache.DieuTriService.KiemTraTonTai(MaBenhNhan, false, true, true, null);
          DataTable dtbDieuTri = dtsDieuTri.Tables[0];
          if (dtbDieuTri.Rows.Count > 0)
            txtNoiDkDieuTri.EditValue = dtbBenhNhan.Rows[0]["TenDonVi"];
          #endregion
        }
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmDkDieuTriOI::FrmDkDieuTriOI_Load", ex, "General", null);
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #region Command Handlers

    private void btnOK_Click(object sender, EventArgs e)
    {
      try
      {
        DieuTri dieuTri = new DieuTri();
        ThayDoiDieuTri thayDoiDieuTri = new ThayDoiDieuTri();
        LichSuDieuTri lichSuDieuTri = new LichSuDieuTri();

        dieuTri.BenhNhanId = BenhNhanId;
        dieuTri.NgayTiepNhanDieuTri = NgayDangKy;
        dieuTri.UserTiepNhan = GlobalCache.Permission.Username;
        dieuTri.MaChamDutDieuTri = null;
        dieuTri.NgayChamDutDieuTri = null;
        dieuTri.DieuTriArv = false;
        dieuTri.MaOpc = GlobalCache.Permission.UnitCode;
        dieuTri.DeleteFlag = false;

        thayDoiDieuTri.BenhNhanId = BenhNhanId;
        thayDoiDieuTri.UserName = GlobalCache.Permission.Username;
        thayDoiDieuTri.NgayThayDoi = DateTime.Today;

        lichSuDieuTri.BenhNhanId = BenhNhanId;
        lichSuDieuTri.OpcGoc = GlobalCache.Permission.UnitCode;
        lichSuDieuTri.MaOpc = GlobalCache.Permission.UnitCode;
        lichSuDieuTri.NgayBatDau = DateTime.Today;

        GlobalCache.DieuTriService.Save(dieuTri);
        GlobalCache.ThayDoiDieuTriService.Save(thayDoiDieuTri);
        GlobalCache.LichSuDieuTriService.Save(lichSuDieuTri);

        MainFormUtils.SetSavedStatus();

        DialogResult = DialogResult.OK;
        Close();

        //For Non-Webservice
        //using (ConnectionScope.CreateTransaction())
        //{
        //  DieuTri dieuTri = new DieuTri();
        //  ThayDoiDieuTri thayDoiDieuTri = new ThayDoiDieuTri();
        //  LichSuDieuTri lichSuDieuTri = new LichSuDieuTri();

        //  dieuTri.NgayTiepNhanDieuTri = deNgayDangKy.DateTime;
        //  dieuTri.UserTiepNhan = GlobalCache.Permission.Username;
        //  dieuTri.MaChamDutDieuTri = null;
        //  dieuTri.NgayChamDutDieuTri = null;
        //  dieuTri.DieuTriArv = false;
        //  dieuTri.MaOpc = GlobalCache.Permission.UnitCode;
        //  dieuTri.DeleteFlag = false;

        //  thayDoiDieuTri.BenhNhanId = BenhNhanId;
        //  thayDoiDieuTri.UserName = GlobalCache.Permission.Username;
        //  thayDoiDieuTri.NgayThayDoi = DateTime.Today;

        //  lichSuDieuTri.BenhNhanId = BenhNhanId;
        //  lichSuDieuTri.OpcGoc = GlobalCache.Permission.UnitCode;
        //  lichSuDieuTri.MaOpc = GlobalCache.Permission.UnitCode;
        //  lichSuDieuTri.NgayBatDau = DateTime.Today;

        //  GlobalCache.DieuTriService.Save(dieuTri);
        //  GlobalCache.ThayDoiDieuTriService.Save(thayDoiDieuTri);
        //  GlobalCache.LichSuDieuTriService.Save(lichSuDieuTri);

        //  if (ConnectionScope.Complete())
        //  {
        //    MainFormUtils.SetSavedStatus();
        //  }
        //  else
        //  {
        //    MainFormUtils.SetSavedFailedStatus();
        //    UIUtils.ShowErrorMessage(Constants.MsgErrSave);
        //  }
        //}
      }
      catch (Exception ex)
      {
        MainFormUtils.SetSavedFailedStatus();
        Logger.LogCritical("FrmDkDieuTriOI::btnOK_Click", ex, "General", null);
        UIUtils.ShowCannotSaveDataMessage();
      }
    }

    #endregion

    #endregion

  }
}